package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.domain.FlowDomain;
import com.dayuanit.dy15.atm.dto.FlowDTO;
import com.dayuanit.dy15.atm.entity.Card;
import com.dayuanit.dy15.atm.entity.Flow;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.holder.PageHolder;
import com.dayuanit.dy15.atm.mapper.CardMapper;
import com.dayuanit.dy15.atm.mapper.FlowMapper;
import com.dayuanit.dy15.atm.service.FlowService;
import com.dayuanit.dy15.atm.util.CardUtil;
import com.dayuanit.dy15.atm.util.DBUtils;
import com.dayuanit.dy15.atm.util.DateUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class FlowServiceImpl implements FlowService {

    @Resource
    private FlowMapper flowMapper;

    @Autowired
    private CardMapper cardMapper;

    public FlowServiceImpl() {
        System.out.println("FlowServiceImpl()");
    }

    @Override
    public PageHolder<List<FlowDTO>> queryFLows(Long cardId, int currentPage) {
        if (null == cardId) {
            throw new BizException("sx, 错啦");
        }

        //总条数
        int total = flowMapper.countFlow(cardId);

        PageHolder<List<FlowDTO>> pageHolder = PageHolder.init(currentPage, total);

        //每页的数据
        List<FlowDomain> flows = flowMapper.listFlow(cardId, pageHolder.offset(), pageHolder.getPageSize());
        List<FlowDTO> flowDTOS = new ArrayList<>(flows.size());//防止数组扩容，优秀 推荐。
        for (FlowDomain flowDomain : flows) {
            FlowDTO flowDTO = new FlowDTO();
            //在set前或者后add都没关系，因为放到集合里的是内存地址。
            flowDTOS.add(flowDTO);

            flowDTO.setAmount(String.valueOf(flowDomain.getAmount() / 100));
            flowDTO.setCardNum(CardUtil.formatNum(flowDomain.getCardNum()));
            flowDTO.setCreateTime(DateUtil.date2String(flowDomain.getCreateTime()));
            flowDTO.setRemark(flowDomain.getRemark());
        }

        //每页的数据
        pageHolder.setData(flowDTOS);

        return pageHolder;
    }

    @Override
    public List<FlowDTO> top10(long userId) {
        //TODO 用户先写死
        List<FlowDomain> flows = flowMapper.top10Flow(userId);
//            List<FlowDTO> dtos = new ArrayList();//普通程序员

        List<FlowDTO> dtos = new ArrayList(flows.size());//优秀程序员；当知道集合要放多少元素是，就给定容量；防止数组扩容

        for (FlowDomain flowDomain :  flows) {
            FlowDTO flowDTO = new FlowDTO();
            //在set前或者后add都没关系，因为放到集合里的是内存地址。
            dtos.add(flowDTO);

            flowDTO.setAmount(String.valueOf(flowDomain.getAmount() / 100));
            flowDTO.setCardNum(CardUtil.formatNum(flowDomain.getCardNum()));
            flowDTO.setCreateTime(DateUtil.date2String(flowDomain.getCreateTime()));
            flowDTO.setRemark(flowDomain.getRemark());
        }

        return dtos;
    }

    @Override
    public List<FlowDTO> listFlow4DownLoad(long userId, long cardId) {
        Card card = cardMapper.selectByPrimaryKey(cardId);
        if (card == null || card.getUserId() != userId) {
            throw new BizException("无权操作");
        }

        List<Flow> flows = flowMapper.listFlow4DownLoad(cardId);
        List<FlowDTO> dtos = new ArrayList<>(flows.size());
        for (Flow flow : flows) {
            FlowDTO flowDTO = new FlowDTO();
            dtos.add(flowDTO);

            flowDTO.setRemark(flow.getRemark());
            flowDTO.setCreateTime(DateUtil.date2String(flow.getCreateTime()));
            flowDTO.setCardNum(card.getCardNum());
            flowDTO.setAmount(String.valueOf(flow.getAmount() / 100.00));
        }

        return dtos;
    }
}
